package org.jetnuke.modules.pictures;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.dao.UserDAO;
import net.jforum.entities.User;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Picture;
import org.jetnuke.entities.PictureComment;
import org.jetnuke.modules.pictures.utils.PictureUtils;

public class ShowMedium implements IAction {
int pid;
	public void setPid(int pid) {
	this.pid = pid;
}
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		Picture picture=DataAccessDriver.getInst().newPictureDAO().retrieveByPK(pid);
		vc.put("picture", picture);
		if(ui!=null && ui.getID()==picture.getUID()){
			vc.put("myPictures",Boolean.TRUE);
		}
		if(picture.getMEDIUMNAME()!=null){
			vc.put("show_normal_link",Boolean.TRUE);
		}
		
		int uid=picture.getUID();
		PictureUtils.manageStyleAndBlocks(vc, uid);
		
		
		
		
		List comments=DataAccessDriver.getInst().newPictureCommentDAO().selectByPicture(pid);
		vc.put("comments",comments);
		Map userMap=new HashMap();
		UserDAO um=DataAccessDriver.getInst().newUserDAO();
		for(int i=0;i<comments.size();i++){
			PictureComment pc=(PictureComment)comments.get(i);
			int author=pc.getMSG_AUTHOR();
			
			Integer itn=new Integer(author);
			if(!userMap.containsKey(itn)){
				User user2=um.selectById(author);
				userMap.put(itn,user2);
			}
		}
		vc.put("userMap",userMap);
		if(picture.getVOTES()>0){
			double average=((double)picture.getPIC_RATING()) /((double) picture.getVOTES());
			vc.put("average",new Double(average));
			int approx=(int)(average+0.5d);
			vc.put("approx",new Integer(approx));
		}else
		{
			vc.put("approx",new Integer(0));
			vc.put("average",new Double(0));
		}
		return new ActionForward(op,info);
	}

}
